﻿Public Class frmSetupReconnaissance
    Private m_arrTime As New ReconnaissanceData
    Private m_frmRecon As New frmReconnaissance
    Private m_reconn As New ReconnaissanceDataConcourse
    Public m_reconnaissance As New ReconnaissanceDataConcourse
    Private m_1 As New ReconnaissanceDataConcourse
    Private m_new As New ReconnDate
    Private m_old As New ReconnDate
    Public SumcountDay As Integer = 0



    Private Sub txtCount_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtCount.Validating

        Try
            If txtCount.Text <> Nothing And txtCount.Text <> "" And txtCount.Text.Trim() <> "" Then
                If txtCount.Text < 1 Then
                    txtCount.Text = ""
                    Me.FlexSetReconnaissance.Rows.Count = 1
                    MessageBox.Show("测试时间不低于一次！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                    Exit Sub
                ElseIf txtCount.Text > 24 Then
                    txtCount.Text = 24
                    Me.FlexSetReconnaissance.Rows.Count = 25
                    For i As Integer = 0 To 23
                        Me.FlexSetReconnaissance.SetData(i + 1, 0, i + 1)
                    Next
                    MessageBox.Show("测试次数不高于 24 次！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                Else
                    Dim L As Integer = 0
                    'Try
                    L = CInt(Me.txtCount.Text)
                    If L < 1 Then

                    Else
                        Me.FlexSetReconnaissance.Rows.Count = L + 1
                        For i As Integer = 0 To L - 1
                            Me.FlexSetReconnaissance.SetData(i + 1, 0, i + 1)
                        Next
                    End If
                End If
            End If
        Catch ex As Exception
            txtCount.Text = ""
            Me.FlexSetReconnaissance.Rows.Count = 1
            MessageBox.Show("输入不正确，测试时间不低于一次，请从新输入数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Exit Sub
        End Try





        Dim p As Integer = 0
        Dim res As DialogResult
        ' 获得新 时间数组，就是表格中的时间数组
        If FlexSetReconnaissance.Rows.Count > 1 Then
            For i As Integer = 0 To Me.FlexSetReconnaissance.Rows.Count - 1 - 1
                ReDim Preserve m_new.ArrTime(i)
                m_new.ArrTime(i) = New ReconnTime
                m_new.ArrTime(i).Rtime = Me.FlexSetReconnaissance.GetData(i + 1, 1)
            Next


            '获得原来数据 中 有几个时间 ，把中几个时间放入另一个时间数组中
            If StringHandle.m_openFile = 1 Then
                If Me.m_reconnaissance.ArrReconnaissance.Length > 0 Then
                    For i As Integer = 0 To Me.m_reconnaissance.ArrReconnaissance.Length - 1
                        Dim shijian As String = ""
                        shijian = Me.m_reconnaissance.ArrReconnaissance(0).Time

                        If shijian = Me.m_reconnaissance.ArrReconnaissance(i).Time Then
                            p += 1
                            If p > 1 Then
                                Exit For
                            Else
                                ReDim Preserve m_old.ArrTime(i)
                                m_old.ArrTime(i) = New ReconnTime
                                m_old.ArrTime(i).Rtime = Me.m_reconnaissance.ArrReconnaissance(i).Time
                            End If
                        Else
                            ReDim Preserve m_old.ArrTime(i)
                            m_old.ArrTime(i) = New ReconnTime
                            m_old.ArrTime(i).Rtime = Me.m_reconnaissance.ArrReconnaissance(i).Time
                        End If
                    Next
                End If


                Dim a As Integer = 0
                ' 观察下看新的时间数组，是否完全保存了老的时间，如果没有就提示数据丢失
                '
                For i As Integer = 0 To m_old.ArrTime.Length - 1
                    For j As Integer = 0 To m_new.ArrTime.Length - 1
                        If m_new.ArrTime(j).Rtime = m_old.ArrTime(i).Rtime Then
                            a += 1
                        End If
                    Next
                Next
                If a < m_old.ArrTime.Length Then
                    res = MessageBox.Show("这样会发生数据丢失，你确定这样做吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                    If res = Windows.Forms.DialogResult.No Then
                        LoadFlex()
                        'Else
                        '    MessageBox.Show("发生数据丢失!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                    End If
                End If
            End If


        End If

    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Dim temp As Integer = 0
        Dim count As Integer = 0
        Dim ttime As String = ""
        Dim tStart As Date = Me.time_Start.Value
        Dim tEnd As Date = Me.time_End.Value

        Dim countDay As Integer = 0

        Dim month As Integer = 0

        Dim c1 As Integer = 0
        Dim nMonth As String = ""
        Dim nDay As String = ""
        Dim nTime As String = ""


        Dim sdatetime As String = ""
        Dim edatetime As String = ""

        Dim nDateTime As String = ""
        Dim oDateTime As String = ""


        Dim DatesYear As String = tStart.Year
        Dim DatesMonth As String = ""
        Dim DatesDay As String = ""
        Dim DateeYear As String = tEnd.Year
        Dim DateeMonth As String = ""
        Dim DateeDay As String = ""
        Dim tcount As Integer = 0

        If Me.txtCount.Text = Nothing And Me.txtCount.Text.Trim() = "" Then
            MessageBox.Show("测试时间最少是一次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub

        End If
        If Me.FlexSetReconnaissance.Rows.Count > 1 Then
            For i As Integer = 0 To Me.FlexSetReconnaissance.Rows.Count - 2
                tcount = 0
                Dim ftime As Integer = CDbl(Me.FlexSetReconnaissance.GetData(i + 1, 1))
                For j As Integer = 0 To Me.FlexSetReconnaissance.Rows.Count - 2
                    If ftime = Me.FlexSetReconnaissance.GetData(j + 1, 1) Then
                        tcount += 1
                    End If
                   
                    If tcount > 1 Then
                        MessageBox.Show("测试时间有两个相等的，请改正！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    End If
                Next
                If CDbl(Me.FlexSetReconnaissance.GetData(i + 1, 1)) < 0 Or CDbl(Me.FlexSetReconnaissance.GetData(i + 1, 1)) > 23 Then
                    ttime = "时间表格中第 " & i + 1 & " 行，时间应该在 0 - 23 之间"
                    MessageBox.Show(ttime, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                End If

            Next
        End If

        count = CInt(Me.txtCount.Text)


        For i As Integer = 0 To count - 1
            ReDim Preserve m_reconnaissance.ArrReconnTime(i)
            m_reconnaissance.ArrReconnTime(i) = New ReconnTime
            m_reconnaissance.ArrReconnTime(i).Rtime = Me.FlexSetReconnaissance.GetData(i + 1, 1)
        Next

        '''' 排序''''''''
        For n As Integer = 0 To Me.m_reconnaissance.ArrReconnTime.Length - 1
            For j As Integer = 0 To Me.m_reconnaissance.ArrReconnTime.Length - 2

                If m_reconnaissance.ArrReconnTime(j).Rtime > m_reconnaissance.ArrReconnTime(j + 1).Rtime Then
                    temp = m_reconnaissance.ArrReconnTime(j + 1).Rtime
                    m_reconnaissance.ArrReconnTime(j + 1).Rtime = m_reconnaissance.ArrReconnTime(j).Rtime
                    m_reconnaissance.ArrReconnTime(j).Rtime = temp
                End If
            Next
        Next



        If Me.time_End.Value < Me.time_Start.Value Then
            MessageBox.Show("结束时间不能小于开始时间！", " 提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub

        ElseIf Me.time_End.Value >= Me.time_Start.Value Then

            '''''''''''' 分开做，老数组 加新时间，新数组 加上全部时间
            ''''''''''''''' 新建个数组 把选择的日期，时间 都放入这个新数组

            While tStart.AddDays(countDay) <= tEnd
                tStart.AddDays(countDay)
                For i As Integer = 0 To Me.m_reconnaissance.ArrReconnTime.Length - 1
                    ReDim Preserve m_1.ArrReconnaissance(c1)
                    m_1.ArrReconnaissance(c1) = New ReconnaissanceData
                    m_1.ArrReconnaissance(c1).Year = tStart.AddDays(countDay).Year

                    If tStart.AddDays(countDay).Month < 10 Then
                        nMonth = "0" & tStart.AddDays(countDay).Month
                    Else
                        nMonth = tStart.AddDays(countDay).Month
                    End If

                    If tStart.AddDays(countDay).Day < 10 Then
                        nDay = "0" & tStart.AddDays(countDay).Day
                    Else
                        nDay = tStart.AddDays(countDay).Day
                    End If

                    If Me.m_reconnaissance.ArrReconnTime(i).Rtime < 10 Then
                        nTime = "0" & Me.m_reconnaissance.ArrReconnTime(i).Rtime
                    Else
                        nTime = Me.m_reconnaissance.ArrReconnTime(i).Rtime
                    End If
                    m_1.ArrReconnaissance(c1).Month = nMonth
                    m_1.ArrReconnaissance(c1).Day = nDay
                    m_1.ArrReconnaissance(c1).Time = nTime
                    c1 += 1
                Next



                countDay += 1
            End While

            If tStart.Month < 10 Then
                DatesMonth = "0" & tStart.Month
            Else
                DatesMonth = tStart.Month
            End If

            If tStart.Day < 10 Then
                DatesDay = "0" & tStart.Day
            Else
                DatesDay = tStart.Day
            End If

            If tEnd.Month < 10 Then
                DateeMonth = "0" & tEnd.Month
            Else
                DateeMonth = tEnd.Month
            End If

            If tEnd.Day < 10 Then
                DateeDay = "0" & tEnd.Day
            Else
                DateeDay = tEnd.Day
            End If

            sdatetime = DatesYear & DatesMonth & DatesDay
            edatetime = DateeYear & DateeMonth & DateeDay
            Dim oDate As String = ""
            If StringHandle.m_boolTaxis = True Then


                For i As Integer = 0 To Me.m_reconnaissance.ArrReconnaissance.Length - 1
                    oDate = Me.m_reconnaissance.ArrReconnaissance(i).Year & Me.m_reconnaissance.ArrReconnaissance(i).Month & Me.m_reconnaissance.ArrReconnaissance(i).Day
                    If CInt(oDate) >= CInt(sdatetime) And CInt(oDate) <= CInt(edatetime) Then
                        oDateTime = Me.m_reconnaissance.ArrReconnaissance(i).Year & Me.m_reconnaissance.ArrReconnaissance(i).Month & Me.m_reconnaissance.ArrReconnaissance(i).Day & Me.m_reconnaissance.ArrReconnaissance(i).Time
                        For j As Integer = 0 To Me.m_1.ArrReconnaissance.Length - 1

                            nDateTime = Me.m_1.ArrReconnaissance(j).Year & Me.m_1.ArrReconnaissance(j).Month & Me.m_1.ArrReconnaissance(j).Day & Me.m_1.ArrReconnaissance(j).Time

                            If oDateTime = nDateTime Then
                                Me.m_1.ArrReconnaissance(j) = Me.m_reconnaissance.ArrReconnaissance(i)
                            End If
                        Next

                    End If

                Next
            Else

                Me.m_reconnaissance = Me.m_1

            End If
            Me.m_reconnaissance = Me.m_1

        End If


        StringHandle.m_start = Me.time_Start.Value
        StringHandle.m_end = Me.time_End.Value







        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        StringHandle.m_start = Me.time_Start.Value
        StringHandle.m_end = Me.time_End.Value
        StringHandle.m_openFile = 1
        StringHandle.m_default = True
        StringHandle.m_boolTaxis = True
        Me.Close()



    End Sub
    ''' <summary>
    ''' 窗体加载事件 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub frmSetupReconnaissance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FlexSetReconnaissance.Styles.Alternate.BackColor = Color.FromArgb(216, 228, 248)

        If StringHandle.m_openFile = 0 Then
            Me.FlexSetReconnaissance.Rows.Count = 1
        End If
        LoadFlex()

        Me.FlexSetReconnaissance.AllowDragging = C1.Win.C1FlexGrid.AllowDraggingEnum.None
        Me.FlexSetReconnaissance.AllowResizing = C1.Win.C1FlexGrid.AllowResizingEnum.None
        Me.FlexSetReconnaissance.AllowSorting = C1.Win.C1FlexGrid.AllowSortingEnum.None
    End Sub
    ''' <summary>
    ''' 初始化表格数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadFlex()
        Dim coun As Integer = 0
        Dim timecount As Integer = 1
        Me.time_Start.Value = StringHandle.m_start
        Me.time_End.Value = StringHandle.m_end

        If StringHandle.m_openFile = 1 Then
            Dim one As String = ""
            Dim sdate As String = Me.m_reconnaissance.ArrReconnaissance(0).Year & Me.m_reconnaissance.ArrReconnaissance(0).Month & Me.m_reconnaissance.ArrReconnaissance(0).Day
            Dim bdate As String = ""
            one = Me.m_reconnaissance.ArrReconnaissance(0).Time
            For i As Integer = 0 To Me.m_reconnaissance.ArrReconnaissance.Length - 1
                bdate = Me.m_reconnaissance.ArrReconnaissance(i).Year & Me.m_reconnaissance.ArrReconnaissance(i).Month & Me.m_reconnaissance.ArrReconnaissance(i).Day
                If sdate = bdate Then
                    coun += 1
                Else
                    Exit For
                End If
            Next

            Me.FlexSetReconnaissance.Rows.Count = coun + 1
            Me.txtCount.Text = coun
            For j As Integer = 0 To coun - 1
                Me.FlexSetReconnaissance.SetData(j + 1, 0, j + 1)
                Me.FlexSetReconnaissance.SetData(j + 1, 1, Me.m_reconnaissance.ArrReconnaissance(j).Time)
                ReDim Preserve m_new.ArrTime(j)
                m_new.ArrTime(j) = New ReconnTime
                m_new.ArrTime(j).Rtime = Me.m_reconnaissance.ArrReconnaissance(j).Time
            Next
        End If

        ' Me.FlexSetReconnaissance.Rows.Count = coun + 1

    End Sub


    Private Sub FlexSetReconnaissance_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles FlexSetReconnaissance.Validating

        Dim p As Integer = 0
        Dim res As DialogResult
        ' 获得新 时间数组，就是表格中的时间数组
        If FlexSetReconnaissance.Rows.Count > 1 Then
            For i As Integer = 0 To Me.FlexSetReconnaissance.Rows.Count - 1 - 1
                ReDim Preserve m_new.ArrTime(i)
                m_new.ArrTime(i) = New ReconnTime
                m_new.ArrTime(i).Rtime = Me.FlexSetReconnaissance.GetData(i + 1, 1)
            Next


            '获得原来数据 中 有几个时间 ，把中几个时间放入另一个时间数组中
            If StringHandle.m_openFile = 1 Then
                If Me.m_reconnaissance.ArrReconnaissance.Length > 0 Then
                    For i As Integer = 0 To Me.m_reconnaissance.ArrReconnaissance.Length - 1
                        Dim shijian As String = ""
                        shijian = Me.m_reconnaissance.ArrReconnaissance(0).Time

                        If shijian = Me.m_reconnaissance.ArrReconnaissance(i).Time Then
                            p += 1
                            If p > 1 Then
                                Exit For
                            Else
                                ReDim Preserve m_old.ArrTime(i)
                                m_old.ArrTime(i) = New ReconnTime
                                m_old.ArrTime(i).Rtime = Me.m_reconnaissance.ArrReconnaissance(i).Time
                            End If
                        Else
                            ReDim Preserve m_old.ArrTime(i)
                            m_old.ArrTime(i) = New ReconnTime
                            m_old.ArrTime(i).Rtime = Me.m_reconnaissance.ArrReconnaissance(i).Time
                        End If
                    Next
                End If


                Dim a As Integer = 0
                ' 观察下看新的时间数组，是否完全保存了老的时间，如果没有就提示数据丢失
                '
                For i As Integer = 0 To m_old.ArrTime.Length - 1
                    For j As Integer = 0 To m_new.ArrTime.Length - 1
                        If m_new.ArrTime(j).Rtime = m_old.ArrTime(i).Rtime Then
                            a += 1
                        End If
                    Next
                Next
                If a < m_old.ArrTime.Length Then
                    res = MessageBox.Show("这样会发生数据丢失，你确定这样做吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                    If res = Windows.Forms.DialogResult.No Then
                        LoadFlex()
                        'Else
                        '    MessageBox.Show("发生数据丢失!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                    End If
                End If
            End If


        End If

    End Sub

    Private Sub FlexSetReconnaissance_AfterEdit(ByVal sender As System.Object, ByVal e As C1.Win.C1FlexGrid.RowColEventArgs) Handles FlexSetReconnaissance.AfterEdit
        Dim ci As Integer = 0
        Dim frow As Integer = 0
        frow = Me.FlexSetReconnaissance.RowSel
        Try
            If CDbl(FlexSetReconnaissance.GetData(frow, 1)) > 1 Then

            End If
            For i As Integer = 0 To Me.FlexSetReconnaissance.Rows.Count - 2
                If i <> frow - 1 Then
                    If CDbl(Me.FlexSetReconnaissance.GetData(frow, 1)) = CDbl(Me.FlexSetReconnaissance.GetData(i + 1, 1)) Then
                        Me.FlexSetReconnaissance.SetData(frow, 1, Me.m_new.ArrTime(frow - 1).Rtime)
                        ' Me.FlexSetReconnaissance.SetData(frow, 1, Me.m_reconnaissance.ArrReconnaissance(frow - 1).Time)
                        MessageBox.Show("有两个时间相等，请改正！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    End If
                End If

            Next
            If CDbl(FlexSetReconnaissance.GetData(frow, 1)) < 0 Or FlexSetReconnaissance.GetData(frow, 1) > 23 Then
                Me.FlexSetReconnaissance.SetData(frow, 1, Me.m_new.ArrTime(frow - 1).Rtime)
                MessageBox.Show("时间应该在 0 点 - 23 点 之间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If
        Catch ex As Exception
            FlexSetReconnaissance.SetData(frow, 1, 0)
        End Try

    End Sub

    Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class
